﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PearTunes.Client.PearTunesServiceReference;
using System.IO;
using System.Collections;
using System.Xml;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


namespace PearTunes.Client
{
    public delegate void PlaylistPositionChangedEventHandler(object sender, EventArgs e);

    public class PlayList : IEnumerable
    {
        public event PlaylistPositionChangedEventHandler PlaylistPositionChanged;

        private List<LocalTrack> playList = new List<LocalTrack>();

        public string Name { get; set; }

        private int playlistposition;
        public int playListPosition
        {
            get { return playlistposition; }
            set
            {
                playlistposition = value;
                OnPlaylistPositionChanged(EventArgs.Empty);
            }
        }

        public PlayList(bool loadDefaultPath)
        {
            playListPosition = -1;
            if (loadDefaultPath)
            {
                List<string> dirs = FileHelper.GetFilesRecursive(Properties.Settings.Default.localMusicPath);

                foreach (string p in dirs)
                {
                    if (Path.GetExtension(p) == ".mp3")
                    {
                        playList.Add(LocalTrackFromFile(p.ToString()));
                    }
                }

                playList.OrderBy(x => x.Artist);
            }
        }
        public PlayList(string fileName)
        {
            playListPosition = -1;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(fileName);
            XmlNodeList trackNodes = xmlDoc.GetElementsByTagName("Track");

            Name = Path.GetFileNameWithoutExtension(fileName);
            
            foreach (XmlNode n in trackNodes)
            {
                if(File.Exists(n.InnerText))
                {
                    playList.Add(LocalTrackFromFile(n.InnerText));
                }
            }
        }

        public bool NextTrack()
        {
            if (playListPosition < playList.Count - 1)
            {
                playListPosition++;
                return true;
            }
            return false;
        }
        public bool PreviousTrack()
        {
            if (playListPosition > 0)
            {
                playListPosition--;
                return true;
            }
            return false;
        }

        private LocalTrack LocalTrackFromFile(string path)
        {
            LocalTrack tempTrack = new LocalTrack();
            TagLib.File temp = TagLib.File.Create(path);

                if (temp.Tag.Title == null)
                {
                    tempTrack.Title = System.IO.Path.GetFileNameWithoutExtension(path);
                }
                else
                {
                    tempTrack.Title = temp.Tag.Title;
                }

                tempTrack.Artist = temp.Tag.FirstPerformer;
                tempTrack.Album = temp.Tag.Album;
                tempTrack.Genre = temp.Tag.FirstGenre;
                tempTrack.Length = temp.Properties.Duration;
                tempTrack.FilePath = path;

            if (temp.Tag.Pictures.Count() > 0)
            {
                tempTrack.Pic = temp.Tag.Pictures.First();
            }
            return tempTrack;
        }
        public IEnumerator GetEnumerator()
        {
            return (IEnumerator)playList.GetEnumerator();
        }

        public LocalTrack CurrentTrack
        {
            get
            {
                return playList[playListPosition];
            }
        }
        public int Count
        {
            get
            {
                return playList.Count;
            }
        }
        public void Add(LocalTrack track)
        {
            playList.Add(track);     
        }
        protected virtual void OnPlaylistPositionChanged(EventArgs e)
        {
            if (PlaylistPositionChanged != null)
            {
                PlaylistPositionChanged(this, e);
            }
        }


    }
}
